package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;

	[SWF(width=465, height=465, backgroundColor=0xffffff, frameRate=60)]
	public class DoubleClickTest extends Sprite
	{

		private var _lastX:Number;

		private var _lastY:Number;

		private var _text:TextField;

		private var _timer:Timer;

		public function DoubleClickTest()
		{
			_text = new TextField();
			_text.multiline = true;
			_text.width = 365;
			_text.height = 465;
			_text.x = 100;
			_text.mouseEnabled = false;
			_text.text = "赤いところをクリックもしくはダブルクリックしてみてください。\n";
			addChild(_text);

			var box:Sprite = new Sprite();
			box.graphics.beginFill(0xff0000);
			box.graphics.drawRect(0, 0, 100, 100);
			box.graphics.endFill();
			box.addEventListener(MouseEvent.CLICK, clickHandler);
			addChild(box);

			_timer = new Timer(400, 1);
			_timer.addEventListener(TimerEvent.TIMER, timerHandler);
		}

		private function clickHandler(e:MouseEvent):void
		{
			var dc:Boolean = false;
			if (_timer.running)
			{
				_timer.stop();
				_timer.reset();
				if (_lastX === e.stageX && _lastY === e.stageY)
				{
					onClick("double", _lastX, _lastY);
					dc = true;
				}
				else
				{
					onClick("single", _lastX, _lastY);
				}
			}

			if (!dc)
			{
				_lastX = e.stageX;
				_lastY = e.stageY;
				_timer.start();
			}
		}

		private function onClick(type:String, x:Number, y:Number):void
		{
			_text.appendText("type=" + type + ", x=" + x + ", y=" + y + "\n");
		}

		private function timerHandler(e:TimerEvent):void
		{
			_timer.stop();
			_timer.reset();
			onClick("single", _lastX, _lastY);
		}
	}
}
